package edu.sysu.cshare.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "T_GROUP")
public class Group {

	@Override
	public String toString() {
		return course.getName() + "-" + ((leader != null) ? (leader.getName())
				: "暂无组长") + "-" + name + members.size() + "人组";
	}

	private Course course;
	private long id;
	private String name;
	private Set<User> members = new HashSet<User>();
	private List<SubmitInfo> submitInfos = new ArrayList<SubmitInfo>();
	private User leader;

	@OneToOne
	@JoinColumn(name = "LEADER_ID")
	public User getLeader() {
		return leader;
	}

	public void setLeader(User leader) {
		this.leader = leader;
	}

	public Group() {

	}

	/*
	 * public boolean hasStudent(User student) { return
	 * members.contains(student); }
	 */

	public Group(Course course, String name) {
		super();
		this.course = course;
		this.name = name;
	}

	public Group(Course course, String name, Set<User> students) {
		super();
		this.course = course;
		this.name = name;
		this.members = students;
	}

	/*
	 * public void addMember(User u) throws Exception { if
	 * (!u.getCourses().contains(this.getCourse())) throw new
	 * Exception("用户没有选择该课程 不能加入组"); if (members.contains(u)) throw new
	 * Exception("用户已存在于该组"); else this.members.add(u); }
	 * 
	 * public void addMembers(List<User> u) throws Exception { if
	 * (!this.getCourse().getStudents().containsAll(u)) throw new
	 * Exception("名单中有人没有选择该课程"); if (members.size() > 0 &&
	 * members.containsAll(u)) throw new Exception("用户已存在"); else
	 * this.members.addAll(u); }
	 * 
	 * public void removeMember(User u) throws Exception { if
	 * (members.contains(u)) members.remove(u); else throw new
	 * Exception("用户不存在"); }
	 * 
	 * public void removeMembers(List<User> u) throws Exception { if
	 * (members.containsAll(u)) members.removeAll(u); else throw new
	 * Exception("用户不存在"); }
	 */
	@ManyToOne
	public Course getCourse() {
		return course;
	}

	@Id
	@GeneratedValue
	public long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	@ManyToMany(targetEntity = User.class)
	@JoinTable(name = "TR_GROUP_USER", joinColumns = @JoinColumn(name = "group_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
	public Set<User> getMembers() {
		return members;
	}

	public SubmitInfo getSubinfoByHomework(Homework homework) {
		for (SubmitInfo sinfo : this.submitInfos) {
			if (sinfo.getHomework().getId() == homework.getId()) {
				return sinfo;
			}
		}
		return null;
	}

	// NOTE 删除一个组 后组里的submitinfo怎么办? 应该是保持..好像比较复杂?
	@OneToMany(mappedBy = "group")
	public List<SubmitInfo> getSubmitInfos() {
		return submitInfos;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	public void setId(long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setMembers(Set<User> students) {
		this.members = students;
	}

	public void setSubmitInfos(List<SubmitInfo> submitInfos) {
		this.submitInfos = submitInfos;
	}
}
